//
//  XWDiscoverSearchView.swift
//  新浪 weibo
//
//  Created by xuwei on 16/1/20.
//  Copyright © 2016年 xuwei. All rights reserved.
//

import UIKit

class XWDiscoverSearchView: UIView {


    @IBOutlet weak var textFieldRightCons: NSLayoutConstraint!
 
  
    @IBOutlet weak var cancelButton: UIButton!


    @IBOutlet weak var textField: UITextField!
    
    class func seachView() -> XWDiscoverSearchView{
        return NSBundle.mainBundle().loadNibNamed("XWDiscoverSeachView", owner: nil, options: nil).last! as! XWDiscoverSearchView
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func awakeFromNib() {
        textField.leftView = leftView
        textField.leftViewMode = .Always
    }
  
    @IBAction func textFieldDidBegin(sender: UITextField) {
        
        self.textFieldRightCons.constant = self.cancelButton.frame.width
        UIView.animateWithDuration(0.25) { () -> Void in
            sender.layoutIfNeeded()
        }
        
        
    }
   
    @IBAction func cancelButtonClick(sender: AnyObject) {
        self.textFieldRightCons.constant = 0
        UIView.animateWithDuration(0.25) { () -> Void in
            self.textField.layoutIfNeeded()
        }
        self.textField.resignFirstResponder()
        
    }
   override func layoutSubviews() {
        super.layoutSubviews()
        leftView.frame.size = CGSizeMake(frame.height, frame.height)
    }
    
    
  
 
    
    private lazy var leftView:UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        imageView.contentMode = .Center
        return imageView
        
    }()
}
